//
// Created by Semir on 2020/4/29.
//

#include <iostream>
#include <vector>
#include <stack>

using namespace std;

struct Node {
    int mineNum;
    int lastNode;
    int maxNum;
};

const int MAX_N = 25;
int n, tmp, maxNum, routeIndex;
vector<Node*> nodes;
vector<vector<int>> mp;
stack<int> route;

int main() {
    cin >> n;
    nodes.resize(n + 1);
    mp.resize(n + 1, vector<int>(n + 1));
    nodes[0] = nullptr;
    for (int i = 1; i <= n; i++) {
        nodes[i] = new Node;
        cin >> nodes[i]->mineNum;
        nodes[i]->lastNode = 0;
        nodes[i]->maxNum = nodes[i]->mineNum;
    }
    for (int i = 1; i < n; i++) {
        for (int j = i + 1; j <= n; j++) {
            cin >> tmp;
            mp[i][j] = tmp == 1 ? 1 : 0;
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++) {
            if (mp[j][i]) {
                if (nodes[j]->maxNum + nodes[i]->mineNum > nodes[i]->maxNum) {
                    nodes[i]->maxNum = nodes[j]->maxNum + nodes[i]->mineNum;
                    nodes[i]->lastNode = j;
                }
            }
        }
    }
    maxNum = 0;
    routeIndex = 0;
    for (int i = 1; i <= n; i++) {
        if (nodes[i]->maxNum > maxNum) {
            maxNum = nodes[i]->maxNum;
            routeIndex = i;
        }
    }
    while (nodes[routeIndex]->lastNode != 0) {
        route.push(routeIndex);
        routeIndex = nodes[routeIndex]->lastNode;
    }
    route.push(routeIndex);
    int flag = false;
    while (!route.empty()) {
        if (flag) {
            cout << " " << route.top();
        } else {
            cout << route.top();
            flag = true;
        }
        route.pop();
    }
    cout << endl << maxNum;
    return 0;
}
